﻿using System;
using System.Web;
using System.Data;
using AliCMS.SystemModule;
using System.Text;

namespace AliCMS.Lib
{
    public class VEditBase : Page
    {
        public VEditBase(string pathInTemplate)
            : base(pathInTemplate)
        {
        }

        protected string BuildList(string pageSize, string instanceInnerID, string objectID, string condition, string sort)
        {
            DataRow[] rows = Manager.Objects.Select(Alias.Module.Object.ID + "='" + objectID + "'");
            string objectName = rows[0]["FullName"].ToString();

            StringBuilder result = new StringBuilder("[");
            using (IObject bll = Manager.CreateObject(objectName, Convert.ToInt32(instanceInnerID), PageLang, false))
            {
                DataTable table = ((IDbObject)bll).List(1, Convert.ToInt32(pageSize), condition, sort);

                System.Collections.Generic.Dictionary<string, string> props = new System.Collections.Generic.Dictionary<string, string>();
                foreach (DataColumn col in table.Columns)
                {
                    rows = Manager.Properties.Select(Alias.Module.Object.Property.ObjectID + "='" + objectID + "' and " + Alias.Module.Object.Property.UniqueInnerID + "='" + col.ColumnName + "'");
                    props[col.ColumnName] = rows[0][Alias.Module.Object.Property.Name].ToString();
                }

                foreach (DataRow row in table.Rows)
                {
                    if (result[result.Length - 1] == '}')
                        result.Append(",");
                    result.Append("{");
                    foreach (DataColumn col in table.Columns)
                    {
                        if (result[result.Length - 1] != '{')
                            result.Append(",");
                        result.Append(props[col.ColumnName] + ":\"" + Manager.Escape(row[col.ColumnName]) + "\"");
                    }
                    result.Append("}");
                }
            }
            result.Append("]");
            return result.ToString();
        }

        #region Properties
        protected DataTable Instances
        {
            get
            {
                DataTable result = new DataTable();
                result.Columns.Add(Alias.Instance.ID, typeof(Guid));
                result.Columns.Add(Alias.Instance.InnerID, typeof(int));
                result.Columns.Add(Alias.Instance.Name, typeof(string));
                result.Columns.Add("Namespace", typeof(string));

                foreach (DataRow row in Manager.Instances.Rows)
                {
                    DataRow newRow = result.NewRow();
                    int i = 0;
                    newRow[i++] = ((Guid)row[Alias.Instance.ID]).ToString("B").ToUpper();
                    newRow[i++] = row[Alias.Instance.InnerID];
                    newRow[i++] = row[Alias.Instance.Name + PageLang];
                    newRow[i++] = row[Alias.Module.Namespace];
                    result.Rows.Add(newRow);
                }

                return result;
            }
        }
        #endregion
    }
}
